'use strict';
app.controller('editInternalNowAccountCtrl',['$scope','$state','$stateParams','accountService','toaster','$modal','$interval',
    function($scope,$state,$stateParams,accountService,toaster,$modal,$interval){
    $scope.isUpdateMobilePhone=0;
    $scope.systemUser = {
        isUpdatePassword:0,
        gender:1
    }

    // query data.
    $scope.findBySessionId=function(){
        accountService.findBySessionId($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.systemUser = result.data;
                    $scope.systemUser.isUpdatePassword = 0;
                    $scope.systemUser.oldMobilePhone=result.data.mobilePhone;
                    $scope.systemUser.phone = result.data.mobilePhone.substring(0,3)+'****'+result.data.mobilePhone.substring(7,11);
                    $scope.systemUser.isUpdateMobilePhone=$scope.isUpdateMobilePhone;
                    break;
                case 601:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }

        });
    }

    $scope.findBySessionId();

    //检查手机号码是否被使用
    $scope.check = function(){
        if(!angular.isUndefined($scope.systemUser.mobilePhone)) {
            accountService.checkPhone($scope).then(function (result) {
                switch (result.status){
                    case 200:
                        $scope.sendSms($scope.systemUser.mobilePhone);
                        break;
                    case 602:
                        toaster.pop('error', "操作失败", "手机号码已被注册");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });
        }
    };

    $scope.description="获取验证码";
    $scope.sendSms = function(mobilePhone){
        $scope.canClick=false;
        var second=59;
        var timerHandler;
        accountService.getMobilePhoneCode($scope,mobilePhone).then(function(result){
            switch (result.status){
                case 200:
                    timerHandler =$interval(function(){
                        if(second<=0){
                            $interval.cancel(timerHandler);    //当执行的时间59s,结束时，取消定时器 ，cancle方法取消
                            second=59;
                            $scope.description="获取验证码";
                            $scope.canClick=false;    //因为 ng-disabled属于布尔值，设置按钮可以点击，可点击发送
                        }else{
                            $scope.description=second+"s后重发";
                            second--;
                            $scope.canClick=true;
                        }
                    },1000)   //每一秒执行一次$interval定时器方法
                    break;
                case 647:
                    toaster.pop('error', "操作失败", "验证码不一致");
                    break;
                case 408:
                    toaster.pop('error', "操作失败", "验证码已过期");
                    break;
                case 680:
                    toaster.pop('error', "操作失败", "不能频繁发送验证码,请稍后再发");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //打开添加框
    $scope.openUpdate = function () {
        var systemUserToken = "mobilePhone="+$scope.systemUser.mobilePhone+
            "&password="+$scope.systemUser.password+"&name="+$scope.systemUser.name+"&email="+$scope.systemUser.email;
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(publicKey);
        $scope.systemUser.systemUserToken = encodeURIComponent(encrypt.encrypt(systemUserToken));
        $scope.isUpdatePwd=$scope.systemUser.isUpdatePassword;

        var modalInstance = $modal.open({
            templateUrl: 'tpl/security/account_mobile_phone_check.html',
            controller: 'updateAccountCtrl',
            size: 'sm',
            scope:$scope,
            resolve: {
               data: function () {
                   var data = $scope.systemUser;
                    return  data;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };
}]);

app.controller('updateAccountCtrl',['$scope','$state','accountService','toaster','data','$modalInstance','$cookies',
    function($scope,$state,accountService,toaster,data,$modalInstance,$cookies){
    $scope.data=data;

    // update event.
    $scope.updateNowAccount = function(){
        accountService.updateNowSystemUser($scope).then(function(result){
            switch (result.status){
                case 200:
                    if($scope.isUpdatePwd == 1 || $scope.isUpdateMobilePhone == 1){
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功,请重新登录");
                        delete $cookies.sessionId;
                        $scope.app.session = {};
                        location.href = $scope.app.loginUrl;
                    }else{
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $state.go('app.home');
                    }
                    break;
                case 602:
                case 603:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };

    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

    $scope.sendSms = function(mobilePhone){
        accountService.getMobilePhoneCode($scope,mobilePhone).then(function(result){
            switch (result.status){
                case 200:
                    break;
                case 647:
                    toaster.pop('error', "操作失败", "验证码不一致");
                    break;
                case 408:
                    toaster.pop('error', "操作失败", "验证码已过期");
                    break;
                case 680:
                    toaster.pop('error', "操作失败", "不能频繁发送验证码,请稍后再发");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }
    $scope.sendSms($scope.data.oldMobilePhone);
}]);